// Vue
import { createApp, h, getCurrentInstance } from "vue";
import Editor from "bin-editor-next";
import App from "@/App.vue";
//json编辑器依赖
import * as ace from "brace";
import "brace/ext/emmet";
import "brace/ext/language_tools";
import "brace/mode/json";
import "brace/snippets/json";
import "brace/theme/cobalt";

// 配置
import Setting from "@/setting";

// 混合
import mixinApp from "@/mixins/app";

// 插件
import plugins from "@/plugins";

// store
import store from "@/store/index";

// ViewUIPlus
import ViewUIPlus from "view-ui-plus";

import "xe-utils"; // 导入 xe-utils，这是使用 vxe-table 所必须的
import VXETable from "vxe-table"; // 导入 vxe-table

// 菜单和路由
import router from "@/router";
import menuHeader from "@/menu/header";
import { frameInRoutes } from "@/router/routes";

// 多语言
import i18n from "@/i18n";
import { loadRemoteLocaleMessages } from "@/i18n/locale";

// 内置组件
import iLink from "@/components/link/index.vue";
import iFrame from "@/components/frame/index.vue";

// 使用样式，修改主题可以在 styles 目录下创建新的主题包并修改 View UI Plus 默认的 less 变量
// 参考 https://www.iviewui.com/docs/guide/theme
import "./styles/index.less";
import "vxe-table/lib/style.css"; // 导入 vxe-table 样式，注意放在 icrud.css 前
import "./libs/icrud/icrud.css";
import iRegion from "@/components/region/region.min";

// 第一次进入时，取菜单数据用于更新
let isFirstRouteChange = true;
// iCRUD 会依赖物料市场里的两个插件：print 和 table-import-export，如果已经购买和配置，则不用重复配置这两个插件
import TableImportExport from "@/libs/icrud/table-import-export/table-import-export.min";
import iPrint from "@/libs/icrud/print/print.min";
// 导入 iCRUD
import iCrud from "@/libs/icrud/icrud.min";
//图标选择器
import iIconSelect from "@/components/icon-select/icon-select.min";
import { getDictionary } from "./api/common";
import "./vxeRegister";

const app = createApp({
  mixins: [mixinApp],
  render: () => h(App),
  created() {
    // 处理路由 得到每一级的路由设置
    this.$store.commit("admin/page/init", frameInRoutes);
    // 设置顶栏菜单
    if (!Setting.dynamicMenu)
      this.$store.commit("admin/menu/setHeader", menuHeader);
    // 加载用户登录的数据
    this.$store.dispatch("admin/account/load");
    // 初始化全屏监听
    this.$store.dispatch("admin/layout/listenFullscreen");
    // 将根实例存全局，可在特殊场景下调用
    if (window) {
      window["$app"] = getCurrentInstance();
      window["$app"].$t = i18n.global.t;
    }
    // 加载远程 i18n
    if (Setting.i18n.remote) loadRemoteLocaleMessages(i18n);
  },
  watch: {
    // 监听路由 控制侧边栏显示 标记当前顶栏菜单（如需要）
    $route(to, from) {
      this.$store.dispatch("admin/menu/setMenuList", to);
      if (to.meta.remoteMenu) {
        if (isFirstRouteChange) {
          isFirstRouteChange = false;
          // 每次进入 app 时，获取一次菜单
          this.$store.dispatch("admin/menu/getMenuList", to);
        }
      } else if (to.meta.subSystem) {
        this.$store.dispatch("admin/menu/getAppNavMenuList", to);
      }
      this.appRouteChange(to, from);
    },
  },
});

app.use(router);
app.use(store);
app.use(i18n);
app.use(ViewUIPlus, {
  i18n,
});
app.use(plugins as any);

app.use(iIconSelect);
app.component(Editor.name, Editor);
app.component("i-link", iLink);
app.component("i-frame", iFrame);

app.mount("#app");
app.use(TableImportExport);
app.use(iPrint);
app.use(VXETable); // 加载 vxe-table 插件
app.use(iCrud, {
  fetchDictFunc: ({ url, dict, options, dialogForm }) => {
    if (dict.code) {
      return getDictionary({ dictType: dict.code }).then((res: any) => {
        return res.map((item: any) => {
          return {
            label: item.dictLabel,
            value: item.dictValue,
          };
        });
      });
    }
  },
});
app.use(iRegion);
